package com.monkeyboy.mytest;

import java.util.HashMap;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyTest1 {
	@Autowired
	private RabbitAdmin rabbitAdmin;
	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Test
	public void testAdmin() throws Exception {
		// 声明交换机
		rabbitAdmin.declareExchange(new DirectExchange("test.direct", false, false));
		rabbitAdmin.declareExchange(new TopicExchange("test.topic", false, false));
		rabbitAdmin.declareExchange(new FanoutExchange("test.fanout", false, false));
		// 声明队列
		rabbitAdmin.declareQueue(new Queue("test.direct.queue", false));
		rabbitAdmin.declareQueue(new Queue("test.topic.queue", false));
		rabbitAdmin.declareQueue(new Queue("test.fanout.queue", false));

		// 绑定队列到交换机上面
		rabbitAdmin.declareBinding(new Binding("test.direct.queue", Binding.DestinationType.QUEUE, "test.direct",
				"direct", new HashMap<>()));
		// 链式绑定方式
		rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue("test.topic.queue", false))
				.to(new TopicExchange("test.topic", false, false)).with("user.#"));
		// fanout类型有点点区别，因为它没有路由键
		rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue("test.fanout.queue", false))
				.to(new FanoutExchange("test.fanout", false, false)));

		// 清空队列里面的未消费的消息
		rabbitAdmin.purgeQueue("test.topic.queue", false);

	}

	@Test
	public void testSendMessage01() throws Exception {
		// 创建消息
		MessageProperties messageProperties = new MessageProperties();
		messageProperties.getHeaders().put("desc", "信息描述");
		// 设置消息内容
		Message message = new Message("hello RabbitMQ".getBytes(), messageProperties);
		// 发送消息
		/**
		 * MessagePostProcessor:前置处理消息 就是
		 */
		rabbitTemplate.convertAndSend("topic001", "spring.amqp", message, new MessagePostProcessor() {
			@Override
			public Message postProcessMessage(Message arg0) throws AmqpException {
				System.err.println("-------添加额外的设置");
				message.getMessageProperties().getHeaders().put("desc", "额外信息修改描述");
				message.getMessageProperties().getHeaders().put("attr", "额外新加是信息");
				return message;
			}
		});
		// rabbitTemplate.setConfirmCallback(confirmCallback);//设置确认监听模式
		// rabbitTemplate.setReturnCallback(returnCallback);//设置return返回模式
	}

	@Test
	public void testSendMessage02() throws Exception {
		// 简单模式发送消息
		rabbitTemplate.convertAndSend("topic001", "spring.amqp", "hello object message send!");
	}

	@Test
	public void testSendMessage03() throws Exception {
		// 创建消息
		MessageProperties messageProperties = new MessageProperties();
		messageProperties.setContentType("text/plain");
		Message message = new Message("mq消息".getBytes(), messageProperties);
		// 多种发送消息的方式
		rabbitTemplate.send("topic001", "spring.123", message);
		// rabbitTemplate.convertAndSend("topic001", "spring.amqp", "hello object
		// message send!");
	}

	/**
	 * 作用： 消息转换器，发送byte消息或者String类型的处理
	 * 
	 * @Author:Gavin
	 * @time:2019年1月17日
	 * @throws Exception
	 */
	@Test
	public void testSendMessage04() throws Exception {
		rabbitTemplate.convertAndSend("topic001", "spring.amqp", "hello object message send!".getBytes());
	}
}
